package com.jflm.controller.api;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jflm.base.BaseAction;
import com.jflm.domain.RuleSet;
import com.jflm.domain.RuleSetData;
import com.jflm.service.IRuleSetDataService;
import com.jflm.service.IRuleSetService;

@RestController
@RequestMapping("/api/ruleSet")
public class RuleSetAction extends BaseAction{
	private static final Logger LOG = LoggerFactory.getLogger(RuleSetAction.class);
	@Autowired
	private IRuleSetService ruleSetService;
	@Autowired
	private IRuleSetDataService ruleSetDataService;
	
	/**
	 * ******************************************** method name : findList
	 * description : 查询全部规则
	 * 
	 * @return : List<RuleData>
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 */
	@PostMapping(value = "/findList")
	public List<RuleSet> findList(@RequestBody RuleSet ruleSet)
			throws Exception {
		//LOG.info("#findList() 请求参数：{}", gson.toJson(ruleData));
		List<RuleSet> reList = ruleSetService.findList(ruleSet);
		//LOG.info("#findList() 响应参数：{}", gson.toJson(reList));
		return reList;
	}
	/**
	 * ******************************************** method name : findList2
	 * description : 查询规则集
	 * 
	 * @return : List<RuleData>
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 */
	@PostMapping(value = "/findList2")
	public List<RuleSet> findList2(@RequestBody RuleSet ruleSet)
			throws Exception {
		LOG.info("#findList2() 请求参数：{}");
		List<RuleSet> reList = ruleSetService.findList2(ruleSet);
		LOG.info("#findList2() 响应参数：{}", gson.toJson(reList));
		return reList;
	}
	/**
	 * ******************************************** method name : findList2
	 * description : 查询规则集详情
	 * 
	 * @return : List<RuleData>
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 */
	@PostMapping(value = "/findList3")
	public List<RuleSet> findList3(@RequestBody RuleSet ruleSet)
			throws Exception {
		LOG.info("#findList() 请求参数：{}");
		List<RuleSet> reList = ruleSetService.findList3(ruleSet);
		LOG.info("#findList() 响应参数：{}", gson.toJson(reList));
		return reList;
	}
	
	
	/**
	 * ******************************************** method name : add
	 * description : 添加规则集
	 * 
	 * @return : 
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 */
	@PostMapping(value="/addPage")
	public int add(@RequestBody RuleSet ruleSet) throws Exception{
		System.out.println(gson.toJson(ruleSet)+"============================");
		UUID uuid = UUID.randomUUID();
		ruleSet.setId(uuid.toString());

		
		Date date = new Date();       
		Timestamp nowdate = new Timestamp(date.getTime()); 
		ruleSet.setCreateTime(nowdate);
		ruleSet.setUpdateTime(nowdate);
		

		
		ruleSet.setDel(0);
		System.out.println(gson.toJson(ruleSet)+"=========================");
		int flag = ruleSetService.add(ruleSet);
		return flag;
	}
	
	/**
	 * ******************************************** method name : add
	 * description : 导入规则集
	 * 
	 * @return : 
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 */
	@PostMapping(value="/datasavePage")
	public int datasavePage(@RequestBody RuleSet ruleSet) throws Exception{
		System.out.println(gson.toJson(ruleSet)+"============================");
		
		String[] dataarr = ruleSet.getDataarr().split(",");
		int flag = 0;
		for (int i = 0; i < dataarr.length; i++) {
			String dataId = dataarr[i];
			String setId =  ruleSet.getSetId();
			UUID uuid = UUID.randomUUID();
			RuleSetData ruleSetData = new RuleSetData();
			ruleSetData.setId(uuid.toString());
			ruleSetData.setDel(0);
			ruleSetData.setDataId(dataId);
			ruleSetData.setSetId(setId);
			
			Date date = new Date();       
			Timestamp nowdate = new Timestamp(date.getTime()); 
			ruleSetData.setCreateTime(nowdate);
			ruleSetData.setCreatorId("admin");
			ruleSetDataService.datasave(ruleSetData);
			/*int ruleCount = */
			/*ruleSet.setRuleCount(2);
			ruleSet.setId(uuid.toString());
			ruleSetService.add(ruleSet);*/
		}
		 
		return flag;
	}
	
	/**
	 * ******************************************** method name : 
	 * description : 删除单条
	 * 
	 * @return : List<RuleSet>
	 * @param : @return
	 * @param : @throws Exception
	 * @see : *******************************************
	 */
	
	@RequestMapping(value = "/deletePage", method = RequestMethod.POST)
	public int deletePage(@RequestBody RuleSet ruleSet) throws Exception {
		int rul = ruleSetService.delete1(ruleSet);
		//LOG.info("#findOne() 响应参数：{}", gson.toJson(ruleData));
		return rul;
	}

}
